package com.attendance.www.hr.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.attendance.www.hr.beans.SalaryApprovalRecordBean;
import com.attendance.www.hr.beans.UserEmployeeBean;
import com.attendance.www.hr.constant.AppConstant;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.IClosedService;
import com.attendance.www.hr.service.ISalaryApprovalService;
import com.zhangkongli.frame.core.ResultMap;

/**
 * 工资--审批
 * 2018-04-12
 * @author 张鑫
 *
 */
@Controller
@RequestMapping("/salaryApproval")
public class SalaryApprovalController {
	
	@Resource
	private IClosedService closedService;
	@Resource
	private ISalaryApprovalService salaryApprovalService;
	
	/**
	 * 批量审批工资报表
	 * @param request
	 * @param salaryApprovalRecordBean
	 * @return
	 */
	@RequestMapping(value = "/saveSalaryApprovalRequestList.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object saveSalaryApprovalRequestList(HttpServletRequest request,@RequestBody SalaryApprovalRecordBean salaryApprovalRecordBean) {
		if (salaryApprovalRecordBean.getSalaryRequestIdList()==null) {
			throw new ResultFaultException("0_00001");
		}
		//获取登陆用户数据  
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if(userEmployee == null){
			throw new ResultFaultException("1_00001");
		}
		Integer employeeId=userEmployee.getEmployeeId();
		salaryApprovalRecordBean.setEmployeeId(employeeId);
		
		return ResultMap.success(salaryApprovalService.saveSalaryApprovalRequestList(salaryApprovalRecordBean));
	}
	
	/**
	 * 查看审批流程信息 by申请人
	 * @param request
	 * @param salaryApprovalRecordBean
	 * @return
	 */
	@RequestMapping(value = "/getSalaryApprovalRecordList.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getSalaryApprovalRecordList(HttpServletRequest request,@RequestBody SalaryApprovalRecordBean salaryApprovalRecordBean) {
		if (salaryApprovalRecordBean.getSalaryRequestId()==null
				||salaryApprovalRecordBean.getSalaryRuleId()==null) {
			throw new ResultFaultException("0_00001");
		}
		//获取登陆用户数据
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if(userEmployee == null){
			throw new ResultFaultException("1_00001");
		}
		Integer employeeId=userEmployee.getEmployeeId();
		salaryApprovalRecordBean.setEmployeeId(employeeId);
		return ResultMap.success(salaryApprovalService.getSalaryApprovalRecordList(salaryApprovalRecordBean));
	}
	
	/**
	 * 判断是否是最终审批人
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getIsLastPeople.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getIsLastPeople(HttpServletRequest request) {
		//获取登陆用户数据
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if(userEmployee == null){
			throw new ResultFaultException("1_00001");
		}
		return ResultMap.success(salaryApprovalService.getIsLastPeople(userEmployee.getEmployeeId()));
	}
	
	/**
	 * 驳回工资审批
	 * @param request
	 * @param salaryApprovalRecordBean
	 * @return
	 */
	@RequestMapping(value = "/saveRejectSalary.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object saveRejectSalary(HttpServletRequest request,@RequestBody SalaryApprovalRecordBean salaryApprovalRecordBean) {
		if (salaryApprovalRecordBean.getSalaryRequestId()==null) {
			throw new ResultFaultException("0_00001");
		}
		//获取登陆用户数据  
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if(userEmployee == null){
			throw new ResultFaultException("1_00001");
		}
		Integer employeeId=userEmployee.getEmployeeId();
		salaryApprovalRecordBean.setEmployeeId(employeeId);
		
		return ResultMap.success(salaryApprovalService.saveRejectSalary(salaryApprovalRecordBean));
	}
}
